package dmt16.TelemetrySuit.UI.Tiles
{
	import dmt16.TelemetrySuit.Controller.SensorTypes.ElectroLycraSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.FlexSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.ISensorType;
	import dmt16.TelemetrySuit.Controller.SensorTypes.SensorEvent;
	import dmt16.TelemetrySuit.UI.Assets.Fonts;
	import dmt16.TelemetrySuit.UI.Assets.PositionTileGFX;
	import dmt16.TelemetrySuit.UI.Colours;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class PositionTile extends TileBase
	{
		public static const LEFT_LEG:Number=0;
		public static const RIGHT_LEG:Number=1;
		
		public function PositionTile(electroLycra:ElectroLycraSensor,flex:FlexSensor,leg:Number)
		{
			_tileGFX=new PositionTileGFX();
			tileContent=_tileGFX;
			
			_tileGFX.width=WIDTH;
			_tileGFX.height=HEIGHT;
			if(leg==LEFT_LEG){
				_lowerLeg=DisplayObjectContainer(_tileGFX.getChildAt(0)).getChildAt(0);
				_upperLeg=DisplayObjectContainer(_tileGFX.getChildAt(0)).getChildAt(1);
				DisplayObjectContainer(_tileGFX.getChildAt(0)).getChildAt(2).visible=false;
				DisplayObjectContainer(_tileGFX.getChildAt(0)).getChildAt(3).visible=false;
			}
			else{
				_lowerLeg=DisplayObjectContainer(_tileGFX.getChildAt(0)).getChildAt(2);
				_upperLeg=DisplayObjectContainer(_tileGFX.getChildAt(0)).getChildAt(3);
				DisplayObjectContainer(_tileGFX.getChildAt(0)).getChildAt(0).visible=false;
				DisplayObjectContainer(_tileGFX.getChildAt(0)).getChildAt(1).visible=false;
			}
			_gtxt=new TextField();
			_gtxt.embedFonts=true;
			_gtxt.selectable=false;
			_gtxt.defaultTextFormat=(leg==LEFT_LEG)?_textFormat2:_textFormat;
			_gtxt.x=0;
			_gtxt.width=WIDTH-WIDTH*2/30;
			_gtxt.height=HEIGHT-HEIGHT*2/30;
			_gtxt.y=HEIGHT*2/30;
			_gtxt.text=(leg==LEFT_LEG)?"LEFT":"Right";
			addChild(_gtxt);
			
			_elycra=electroLycra;
			_flex=flex;
			
			_setAngle(0,0);
			
			flex.addEventListener(SensorEvent.SENSOR_DATA_EVENT,_onSensorEvent);
		}
		
		private static var _textFormat:TextFormat;
		{
			_textFormat=new TextFormat();
			_textFormat.font=Fonts.DSDigitalBold;
			_textFormat.size=HEIGHT*4.5/30;
			_textFormat.align=TextFormatAlign.RIGHT;
			_textFormat.color=Colours.C4;
		}
		private static var _textFormat2:TextFormat;
		{
			_textFormat2=new TextFormat();
			_textFormat2.font=Fonts.DSDigitalBold;
			_textFormat2.size=HEIGHT*4.5/30;
			_textFormat2.align=TextFormatAlign.RIGHT;
			_textFormat2.color=Colours.C3;
		}
		
		private var _tileGFX:PositionTileGFX;
		private var _gtxt:TextField;
		private var _lowerLeg:DisplayObject;
		private var _upperLeg:DisplayObject;
		private var _elycra:ElectroLycraSensor;
		private var _flex:FlexSensor;
		
		public function _onSensorEvent(event:Event):void{
			_setAngle(_elycra.data[_elycra.data.length-1],_flex.data[_flex.data.length-1]);
		}
		
		public function _setAngle(upper:Number,lower:Number):void{
			var mupper:Matrix=new Matrix();
			var mlower:Matrix=new Matrix();
			mlower.rotate(lower);
			mlower.translate(0,58.552);
			mupper.rotate(-upper);
			mupper.translate(WIDTH*4/30,HEIGHT/10);
			mlower.concat(mupper);
			_lowerLeg.transform.matrix=mlower;
			_upperLeg.transform.matrix=mupper;
		}
	}
}